//
//  ZYMaximumErrorController.m
//  BestLearning
//
//  Created by ちょうかんこん on 2017/12/19.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "ZYMaximumErrorController.h"
#define COOKIE @"client.xing6688.com"
#import <MediaPlayer/MediaPlayer.h>
#import <JavaScriptCore/JavaScriptCore.h>
#define WeakSelf(weakSelf)  __weak __typeof(&*self)weakSelf = self;
@interface ZYMaximumErrorController ()<UIWebViewDelegate>
@property (nonatomic,strong) UIWebView *webView;
@property (nonatomic,strong) UIWebView *palyWebView;
@property (nonatomic,strong) JSContext *context;

@end

@implementation ZYMaximumErrorController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    NSString *url  = [NSString stringWithFormat:@"yxhUser.do?action=getCompetitionDetial&lessonId=%@",self.lessonId];
    
    [self loadWeb:LLBaseURL(url)];
   
}

- (UIWebView *)palyWebView{
    
    if (  _palyWebView == nil) {
        self.palyWebView = [[UIWebView alloc]initWithFrame:CGRectMake(1, 1, 1, 1)];
        self.palyWebView.delegate =self;
        [self.view addSubview:self.palyWebView];
    }
    
    return _palyWebView;
    
    
    
}


- (void)loadWeb:(NSString *)url {
    
    [SVProgressHUD setDefaultStyle:(SVProgressHUDStyleCustom)];
    [SVProgressHUD setBackgroundColor:ColorRGB(4, 4, 4)];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    [SVProgressHUD showWithStatus:LLLoading];
    
    self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64)];
    NSString *userAgent = [self.webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
    //    NSMutableString* user =  [[NSMutableString alloc]initWithString:userAgent];
    //      [user insertString:@";YXH-Client"atIndex:53];
    NSString *newUserAgent = [userAgent stringByAppendingString:@";YXH-Client"];
    [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent":newUserAgent}];
    
    
    //
    self.webView.delegate = self;
    self.webView.scalesPageToFit = YES;
    [self.view addSubview:self.webView];
    

    // 清空 cookie
    NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    NSArray *cookieArray = [NSArray arrayWithArray:[cookieJar cookies]];
    for (id obj in cookieArray) {
        [cookieJar deleteCookie:obj];
    }
    
    
    
    
    
    // 手动设置cookie
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    
    NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
    [cookieProperties setValue:@"uid" forKey:NSHTTPCookieName];
    [cookieProperties setValue:[[NSUserDefaults standardUserDefaults] valueForKey:@"authCode"] forKey:NSHTTPCookieValue];
    [cookieProperties setValue:COOKIE forKey:NSHTTPCookieDomain];
    [cookieProperties setValue:COOKIE forKey:NSHTTPCookieOriginURL];
    
    [cookieProperties setValue:@"/" forKey:NSHTTPCookiePath];
    [cookieProperties setValue:@"0" forKey:NSHTTPCookieVersion];
    [cookieProperties setValue:[NSDate dateWithTimeIntervalSinceNow:60*60] forKey:NSHTTPCookieExpires];//设置失效时间
    [cookieProperties setValue:@"0" forKey:NSHTTPCookieDiscard]; //设置sessionOnly
    
    NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
    
    NSLog(@"ddddd%@   /n ggggggggg%@",cookieProperties ,cookie);
    
    [self.webView loadRequest:request];
    
}



- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [SVProgressHUD dismiss];
    
   WeakSelf(weakSelf)
    dispatch_async(dispatch_get_main_queue(), ^{
        self.context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
        //定义好JS要调用的方法, share就是调用的share方法名
      
        
      
        
        
        self.context[@"playVideo"] = ^(NSString *str){
            
            [weakSelf SetupVideoPlayer:str];
        };
        
        
        
        
        
        
    });
    
}



- (void)SetupVideoPlayer:(NSString *)str{
    
    
    self.palyWebView = [[UIWebView alloc]initWithFrame:CGRectMake(1,1,1,1)];
    self.palyWebView.delegate = self;
    [self.palyWebView loadRequest:[[NSURLRequest alloc]initWithURL:[NSURL URLWithString:str]]];
    self.palyWebView.hidden =NO;
    
    [self.view addSubview:self.palyWebView];
    
}







- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    [SVProgressHUD dismiss];
   
    if (self.Back) {
        self.Back();
    }
    
}



@end
